﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;

public partial class GiaoDienClient_TrangTinTucChiTiet : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    string MaTin;
    string MaChuyenMuc;
    protected void Page_Load(object sender, EventArgs e)
    {
        MaTin = Request.QueryString["MaTin"];
        MaChuyenMuc = Request.QueryString["MaChuyenMuc"];
        if (!IsPostBack)
        {
            LoadDuLieu();
            LoadBaiTuongTu();
            LoadTinTucHocTap();
            LoadTinTucMoi();
            //if (dtlTuongTu.Items.Count > 0)
            //{
            //    CustomPaging page = new CustomPaging();
            //    DataTable dt = new DataTable();
            //    dt.Columns.Add("MaTin", typeof(string));
            //    dt.Columns.Add("TieuDe", typeof(string));
            //    dt.Columns.Add("NgayDang", typeof(string));
            //    dt.Columns.Add("MaChuyenMuc", typeof(string));
            //    if (MaChuyenMuc != "" && MaTin != "")
            //    {
            //        var c = from p in db.TinTucs where p.MaChuyenMuc == int.Parse(MaChuyenMuc) select new { p.TieuDe, p.NgayDang, p.MaTin, p.MaChuyenMuc };
            //        foreach (var c1 in c)
            //        {
            //            DataRow dr = dt.NewRow();
            //            dr[0] = c1.MaTin.ToString();
            //            dr[1] = c1.TieuDe;
            //            dr[2] = c1.NgayDang.Value.ToShortDateString();
            //            dr[3] = c1.MaChuyenMuc.ToString();
            //            dt.Rows.Add(dr);
            //        }
            //        dtlTuongTu.DataSource=dt;
            //        dtlTuongTu.DataBind();
            //    }
            //    page.PageSize = 5;
            //    page.DataSource =dt;
            //    page.CurrentPageIndex = 0;
            //    Session["DT"] = dt;
            //    Session["page"] = page;
            //    bindData(page, dt);
            //}
            //dtlTuongTu.RepeatColumns = 5;
            //dtlTuongTu.RepeatDirection = RepeatDirection.Horizontal;
        }

    }
    //string NavigationIndicator()
    //{
    //    string str = string.Empty;     //Page x Of Y
    //    str = "Trang:" + Convert.ToString(((CustomPaging)Session["page"]).CurrentPageIndex + 1) + " của " + ((CustomPaging)Session["PAGE"]).PageCount.ToString();
    //    return str;
    //}

    //void bindData(CustomPaging page, DataTable dt)
    //{
    //    try
    //    {
    //        dtlTuongTu.DataSource = page.DoPaging;
    //        dtlTuongTu.DataBind();
    //        lbtnPre.Enabled = !page.IsFirstPage; //Enable / Disable Navigation Button
    //        lbtnNext.Enabled = !page.IsLastPage;
    //        lblStatus.Text = NavigationIndicator(); //Build Navigation Indicator 

    //        DataTable dt1 = new DataTable();
    //        dt1.Columns.Add("PageIndex");
    //        dt1.Columns.Add("PageText");

    //        for (int i = 0; i < page.PageCount; i++)
    //        {
    //            DataRow dr = dt1.NewRow();
    //            dr[0] = i;
    //            dr[1] = i + 1;
    //            dt1.Rows.Add(dr);
    //        }

    //        dlPaging.DataSource = dt1;
    //        dlPaging.DataBind();
    //    }
    //    catch (Exception)
    //    {
    //    }
    //    finally
    //    {
    //        page = null;
    //    }
    //}

    void LoadTinTucHocTap()
    {
        dtlGD.DataSource = db.TinGiaoDuc();
        dtlGD.DataBind();
    }
    void LoadTinTucMoi()
    {
        dtlMoi.DataSource = db.TinMoi();
        dtlMoi.DataBind();
    }
    void LoadDuLieu()
    {
        if (MaTin != "")
        {
            var c = from p in db.TinTucs where p.MaTin == int.Parse(MaTin) select new { p.TieuDe, p.NoiDung, p.Anh, p.TomTatNoiDung, p.NgayDang, p.MaChuyenMuc, p.MaTin };
            dtlNDChiTiet.DataSource = c;
            dtlNDChiTiet.DataBind();
        }
    }

    void LoadBaiTuongTu()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("MaTin", typeof(string));
        dt.Columns.Add("TieuDe", typeof(string));
        dt.Columns.Add("NgayDang", typeof(string));
        dt.Columns.Add("MaChuyenMuc", typeof(string));
        if (MaChuyenMuc != "" && MaTin != "")
        {
            var c = from p in db.TinTucs where p.MaChuyenMuc == int.Parse(MaChuyenMuc) select new { p.TieuDe, p.NgayDang, p.MaTin, p.MaChuyenMuc };
            foreach (var c1 in c)
            {
                DataRow dr = dt.NewRow();
                dr[0] = c1.MaTin.ToString();
                dr[1] = c1.TieuDe;
                dr[2] = c1.NgayDang.Date.ToShortDateString();
                dr[3] = c1.MaChuyenMuc.ToString();
                dt.Rows.Add(dr);
            }
        }
        dtlTuongTu.DataSource = dt;
        dtlTuongTu.DataBind();

    }

    //protected void lbtnPre_Click(object sender, EventArgs e)
    //{
    //    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    //    if (!((CustomPaging)Session["page"]).IsFirstPage)
    //        //Decrements the pageIndex by 1 //(Move to Previous page)
    //        ((CustomPaging)Session["page"]).CurrentPageIndex -= 1;
    //    else
    //        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //   // Binds the DataList with new pageIndex
    //    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
    //}
    //protected void lbtnNext_Click(object sender, EventArgs e)
    //{
    //    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    //    if (!((CustomPaging)Session["page"]).IsLastPage)
    //       // Increments the pageIndex by 1 (Move to Next page)
    //        ((CustomPaging)Session["page"]).CurrentPageIndex += 1;
    //    else
    //        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //   // Binds the DataList with new pageIndex           
    //    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
    //}
}